/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package indexfileprocessor;

import com.sun.corba.se.impl.orb.ParserTable.TestAcceptor1;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Zakiy Firdaus
 */
public class NestedIndexWriter {

    public NestedIndexWriter(){
        
    }
    
    private RandomAccessFile rafRead;
    private RandomAccessFile rafWrite;
    
    public void OpenFile(String pathR, String pathW){
        File fileR = new File(pathR);
        if (!fileR.exists()) {
            try {
                fileR.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(NestedIndexWriter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            //Open the file for both reading and writing
            this.rafRead = new RandomAccessFile(fileR, "rw");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        
        File fileW = new File(pathW);
        if (!fileW.exists()) {
            try {
                fileW.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(NestedIndexWriter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            //Open the file for both reading and writing
            this.rafWrite = new RandomAccessFile(fileW, "rw");
            rafWrite.setLength(0);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
    
    public void closeAllFile(){
        try {
            rafRead.close();
        } catch (IOException ex) {
            Logger.getLogger(NestedIndexWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rafWrite.close();
        } catch (IOException ex) {
            Logger.getLogger(NestedIndexWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void writeFirstNestedIndex(String pathR, String pathW) {
        OpenFile(pathR, pathW);
        try {
            rafRead.seek(0);
            String sCurrentLine = rafRead.readLine();
            String token ="";
            long posisitoken=0;
            while(sCurrentLine!=null){
                if(sCurrentLine.equals("@@")){
                    posisitoken = rafRead.getFilePointer();
                    sCurrentLine = rafRead.readLine();
                    token = sCurrentLine+" "+posisitoken+"\n";
                    rafWrite.seek(rafWrite.length());
                    rafWrite.writeBytes(token);
                }
                sCurrentLine = rafRead.readLine();
            }
            
        } catch (IOException ex) {
            Logger.getLogger(NestedIndexWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        closeAllFile();
    }
    
    public void writeNextNestedIndex(String pathR, String pathW, int jumlahPembagian) {
        OpenFile(pathR, pathW);
        try {
            rafRead.seek(0);
            String sCurrentLine = rafRead.readLine();
            String token ="";
            long posisitoken=0;
            int iterator=jumlahPembagian;
            while(sCurrentLine!=null){
                if(iterator==jumlahPembagian){
                    iterator=0;
                    token = sCurrentLine.substring(0,sCurrentLine.indexOf(" "));
                    token = token+" "+posisitoken+"\n";
                    rafWrite.seek(rafWrite.length());
                    rafWrite.writeBytes(token);
                }
                iterator++;
                posisitoken = rafRead.getFilePointer();
                sCurrentLine = rafRead.readLine();
            }
            
        } catch (IOException ex) {
            Logger.getLogger(NestedIndexWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        closeAllFile();
    }
    
    public void testMakeInitialIndex(String pathR, String pathW, int jumlahtoken){
        OpenFile(pathR, pathW);
        for(int i=0;i<jumlahtoken;i++){
            String content = "@@\ntoken#"+i+"\n0"+i+"\n1"+i+"\n2"+i+"\n3"+i+"\n";
            try {
                this.rafRead.seek(rafRead.length());
                rafRead.writeBytes(content);
                
                
            } catch (IOException ex) {
                Logger.getLogger(NestedIndexWriter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        closeAllFile();
    }
    
    //FUNGSI UTAMA
    public void MakeNestedIndexFile(String rawIndexFile, String NestedIndexFile1, String NestedIndexFile2, String NestedIndexFile3){
        writeFirstNestedIndex(rawIndexFile, NestedIndexFile1);
        writeNextNestedIndex(NestedIndexFile1, NestedIndexFile2, 1000);
        writeNextNestedIndex(NestedIndexFile2, NestedIndexFile3, 50);
    }
    
    public static void main(String[] args){
        NestedIndexWriter niw = new NestedIndexWriter();
    }
}
